<?php
class User_model extends CI_Model
{
	public $tbl = 'user';

	public function __contruct()
	{
		parent::__construct();	
	}

	public function All($companyId)
	{
		$this->db->where('CompanyId', $companyId);
		$query = $this->db->get($this->tbl);
		
		return $query->result();
	}

	public function Register($companyId)
	{
		$data = array(
			'Email' => $this->input->post('Email'),
			'Username' => $this->input->post('Username'),
			'Password' => md5($this->input->post('Password')),
			'CompanyId' => $companyId
			);

		return $this->db->insert($this->tbl, $data);
	}

	public function Login()
	{
		$this->db->where('Username', $this->input->post('Username'));
        $this->db->where('Password', md5($this->input->post('Password')));
        $this->db->where('IsActive', true);

        $query = $this->db->get($this->tbl);
		if ($query->num_rows()) {

			$this->session->set_userdata('userId', $query->row()->Id);
			$this->session->set_userdata('companyId', $query->row()->CompanyId);
			
		 	return true;
		}else{
			return false;
		} 
	}

	public function Active($userId)
	{
		$queryString = "UPDATE user
						SET IsActive = CASE WHEN IsActive = 1 THEN 0 ELSE 1 END
						WHERE id = $userId";
		$query = $this->db->query($queryString);		
	}
}
?>